﻿using EasyCore.Caching.Interfaces;
using IdentityModel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.App
{
    /// <summary>
    /// 全局访问类
    /// </summary>
    public static class EasyApp
    {
        /// <summary>
        /// 当前应用构造器
        /// </summary>
        public static WebApplicationBuilder ApplicationBuilder { get; set; }

        /// <summary>
        /// 当前应用程序
        /// </summary>
        public static WebApplication WebApplication { get; set; }

        /// <summary>
        /// 当前应用程序的环境
        /// </summary>
        public static IWebHostEnvironment HostEnvironment
        {
            get
            {
                if (ApplicationBuilder != null) return ApplicationBuilder.Environment;
                return null;
            }
        }

        /// <summary>
        /// 根依赖注入服务
        /// </summary>
        public static IServiceProvider? Root { get; set; }

        /// <summary>
        /// 当前请求依赖注入服务
        /// </summary>
        public static IServiceProvider? RequestService
        {
            get
            {
                if (HttpContextAccessor != null && HttpContextAccessor.Value != null) return HttpContextAccessor.Value.HttpContext.RequestServices;
                if (Root != null) {
                    var score = Root.CreateScope().ServiceProvider;
                    return score;                
                }
                return null;
            }
        }

        /// <summary>
        /// 当前请求上下文
        /// </summary>
        public static AsyncLocal<IHttpContextAccessor> HttpContextAccessor { get; set; } = new AsyncLocal<IHttpContextAccessor>();

        /// <summary>
        /// 全局配置文件
        /// </summary>
        public static IConfiguration Configuration
        {
            get
            {
                if (WebApplication != null) return WebApplication.Configuration;
                if (ApplicationBuilder != null) return ApplicationBuilder.Configuration;
                return new ConfigurationBuilder().Build();
            }
        }

        /// <summary>
        /// 缓存对象
        /// </summary>
        public static IEasyCacheService Cache
        {
            get
            {
                var cacheService = RequestService.GetRequiredService<IEasyCacheService>();
                return cacheService;
            }
        }

        /// <summary>
        /// 当前请求的唯一标识
        /// </summary>
        public static HttpContext? HttpContext
        {
            get
            {
                if (HttpContextAccessor == null || HttpContextAccessor.Value == null) return null;
                return HttpContextAccessor.Value.HttpContext;
            }
        }

        /// <summary>
        /// 用户唯一标识
        /// </summary>
        public static long UserId
        {
            get
            {
                if (HttpContext == null) return 0;
                var idStr = HttpContext.User.FindFirstValue(JwtClaimTypes.Id);
                if (string.IsNullOrWhiteSpace(idStr)) return 0;
                return long.Parse(idStr);
            }
        }

        /// <summary>
        /// </summary>
        /// </summary>
        public static string UserName
        {
            get
            {
                if (HttpContext == null) return "匿名";
                var name = HttpContext.User.FindFirstValue(JwtClaimTypes.Name);
                if (string.IsNullOrWhiteSpace(name)) name = "匿名";
                return name;
            }
        }
    }
}
